/*
 * @Author: 贺楠 1071780426@qq.com
 * @Date: 2024-09-13 13:21:57
 * @LastEditors: 陈妮娜 969682281@qq.com
 * @LastEditTime: 2024-10-12 09:58:36
 * @FilePath: \monitoring_system\src\components\testMachineManage\testNameManage\index.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

import publicForm from "@/components/public/el-forms.vue"; //弹框form
import formJs from "./regexDetail.js"; //新增弹框配置项
import CustomDialog from "@/components/testMachineManage/testType/index.vue";
import publicForms from "@/components/testMachineManage/testNameManage/el-forms.vue"; //弹框form

import {
    saveMachineTesttype, //保存
    getMachineTesttype, //详情
    getBaseDataitemDetailByItemCode //字典项

} from "@/api/testMachineManage/request.js";
export default {
    props: [
        "dialogVisible",
        "dialogTitle",
        "statusNum",
        "currentRow",
        "typ"
    ],
    data() {
        return {
            visible: "",
            loading: false,
            title: null,
            isLock: false,
            dialogHeight: null,
            treeData: [], //材料种类
            formData: {
                fId: "",
                fTestno: "", //试验编号
                fSampletype: "", //样品类型
                fSampletypeid: "", //样品类型code
                fTestname: "", //试验名称
                fTestshortname: "", //试验简称
                fTestcode: "", //对接代码
                fTestbasis: "", //试验依据
                fIsuse: "", //是否启用
                fIsqrcode: "", //是否二维码
                fIsmeasured: "", //是否启用实测尺寸
                fIsuses: true, //是否启用
                fIsqrcodes: true, //是否二维码
                fIsmeasureds: true, //是否启用实测尺寸
                fTem: "", //温度
                fHum: "", //湿度
                fPiecenum: "", //一组试件数量：
                fAllpiecenum: "", //试件总数量：
                fIsattenuationloadrate: "", //是否有荷载衰减率
                fAttenuationloadrate: "", //荷载衰减率
                fIsattenuationload: "", //是否有衰减荷载

                fAttenuationload: "", //衰减荷载
                fJudgmenttype: "", //起判荷载（N） / 起判强度MPa
                fJudgmentnum: "", //起判值
                fForcesensors: "", //力值传感器
                fForceunit: "", //力值单位
                fForcerepair: "", //力值修约
                fIntensityrepair: "", //强度修约
                fStartvalue: "", //起画值(N)
                fReturnspeed: "", //返车速度(mm/min)
                fUnitadjustments: "", //实时数据单位调整
                fDecide: "", //判定方法
                fEndaction: "", //结束后动作
                fAreacalculation: "", //面积计算
                fAreacalculationnote: "", //面积计算备注
                fIntensitycalculations: "", //强度计算
                fIntensitycalculationsnote: "", //强度计算备注
                itemsList: []

            },
            formObj: formJs.formObj,
            btnShow: true,
            currentDetail: null,
            isShow: false,
            tableData: [],
            tableObj: formJs.tableObj,

            fTesttypeData: [], //字典项名称

        };
    },
    components: {
        publicForm,
        CustomDialog,
        publicForms

    },
    watch: {
        dialogVisible: {
            handler(newVal) {
                if (newVal) {
                    this.visible = newVal;
                }
            },
            immediate: true,
        },
        dialogTitle: {
            handler(newVal) {
                if (newVal) {
                    this.title = newVal;
                }
            },
            immediate: true,
        },

        statusNum: {
            handler(newVal) {
                if (newVal) {

                    this.edit(newVal);
                }
            },
            immediate: true,
        },
        currentRow: {
            handler(newVal) {
                if (newVal) {
                    this.currentDetail = newVal;
                    if (this.statusNum != 1) {
                        this.getDetail({
                            fId: this.currentDetail.fId
                        })
                    }
                }
            },
            immediate: true,
        },
    },
    created() {
        window.addEventListener("resize", this.getHeight);
        this.getHeight();
    }, //获取页面高度自适应：
    destroyed() {
        window.removeEventListener("resize", this.getHeight);
        this.getHeight();
    },
    mounted() {
        this.userInfo = JSON.parse(sessionStorage.getItem("monitorBack-userInfo"));
        this.btnShow = true
        if (this.statusNum == 1) { //新增
            this.formObj.map((item) => {
                item.disabled = false;
            });
            this.tableObj.map((item) => {
                item.disabled = false;
            });
            this.btnShow = true
        }
        if (this.statusNum == 2) { //编辑
            this.formData.fId = this.currentDetail.fId //按钮 
            this.formObj.map((item) => {
                item.disabled = false;
            });
            this.tableObj.map((item) => {
                item.disabled = false;
            });
            this.btnShow = true
        } else if (this.statusNum == 7) { //查看
            this.btnShow = false
            this.formObj.map((item) => {
                item.disabled = true;
            });
            this.tableObj.map((item) => {
                item.disabled = true;
            });
        }
        this.getHeight();
    },
    methods: {
        getHeight() {
            var h = document.documentElement.clientHeight; //获取页面可见高度
            this.dialogHeight = h * 0.76 + "px";
        },
        handleSelect(key) {
            this.activeIndex = key;
        },
        closeDialog() {
            this.$emit("close");
        },
        // 新增试验属性
        addtMachineTesttype() {
            this.isShow = true;
        },
        testDialog() {
            this.isShow = false;
        },
        getTestType(data) { //获取试验属性
            this.getBaseDataitemDetailByItemCode('SYSX', 1)
            this.tableData = data;
        },
        refreshList() { },
        // 新增试验属性
        selectChange(data, index, prop) {
            if (prop == 'fSampletype') {
                this.formData.fSampletype = data.label;
                this.formData.fSampletypeid = data.value;
                this.getBaseDataitemDetailByItemCode(data.value, 0) //试验名称
            } else if (prop == 'fTestname') {
                this.formData.fTestname = data.label;
                this.formData.fTestshortname = data.text;
                this.formData.fTestcode = data.value;
            }
        },
        selectEquipment(row, value, index) {
            this.tableData[index].fTesttypedictcode = value;
        },
        edit() {
            this.formData = this.$options.data.call(this).formData;
        },

        getBaseDataitemDetailByItemCode(status, num) {
            getBaseDataitemDetailByItemCode({
                fItemcode: status
            }).then(res => {
                var data = res.data.baseDataitemdetailList;
                if (num == 0) { //试验名称
                    this.formObj[2].options = [];
                    this.fTesttypeData = [{
                        label: "请选择", //项目名  试验名称
                        value: "", //对接代码
                    }]
                    data.map(item => {
                        this.formObj[2].options.push({
                            label: item.fItemname, //项目名  试验名称
                            value: item.fItemvalue, //对接代码
                            text: item.fDescription //项目值  试验简称

                        })
                    })
                } else if (num == 1) {
                    data.map(item => {
                        this.fTesttypeData.push({
                            label: item.fItemname, //项目名  试验名称
                            value: item.fItemvalue, //对接代码
                        })
                    })
                }

            })
        },
        // 详情
        getDetail(obj) {
            this.loading = true;
            getMachineTesttype(obj).then((res) => {
                if (res.errcode == 0) {
                    var data = res.data.yjQmpMachineTesttype;

                    if (data.fIsuse == '是') {
                        data['fIsuses'] = true;
                    } else if (data.fIsuse == "否") {
                        data['fIsuses'] = false;
                    }
                    if (data.fIsqrcode == '是') {
                        data['fIsqrcodes'] = true;
                    } else if (data.fIsqrcode == "否") {
                        data['fIsqrcodes'] = false;
                    }
                    if (data.fIsmeasured == '是') {
                        data['fIsmeasureds'] = true;
                    } else if (data.fIsmeasured == "否") {
                        data['fIsmeasureds'] = false;
                    }

                    this.tableData = data.itemsList;
                    if (this.statusNum == 2) {
                        this.getBaseDataitemDetailByItemCode('SYSX', 1)
                    }

                    this.formData = data;
                    this.loading = false;

                    if (this.statusNum == 7) {
                        this.btnShow = false;
                    } else {
                        this.btnShow = true;
                    }
                } else {
                    this.loading = false;
                    this.$message.error(res.msg);
                }
            });
        },
        yj_saveForm() {
            this.formData.itemsList = this.tableData;
            // 0连续新增1新增
            if (this.$refs.publicForm.submitForm()) {
                if (this.formData.fIsuses == true) {
                    this.formData.fIsuse = "是";
                } else if (this.formData.fIsuses == false) {
                    this.formData.fIsuse = "否";

                }
                if (this.formData.fIsqrcodes == true) {
                    this.formData.fIsqrcode = "是";
                } else if (this.formData.fIsqrcodes == false) {
                    this.formData.fIsqrcode = "否";
                }
                if (this.formData.fIsmeasureds == true) {
                    this.formData.fIsmeasured = "是";
                } else if (this.formData.fIsmeasureds == false) {
                    this.formData.fIsmeasured = "否";

                }
                this.saveForm(this.formData);
            } else {
                console.log("error");
            }
        },
        // 保存
        saveForm(data) {
            this.loading = true;

            this.isLock = true
            saveMachineTesttype(data)
                .then((res) => {
                    if (res.errcode == 0) {
                        this.loading = false;
                        this.visible = false;
                        this.$emit("refreshList");
                        this.$message.success("新增成功");
                        this.isLock = false
                    } else {
                        this.loading = false;
                        this.isLock = false
                        this.$message.error(res.data);
                    }
                })
        },
    },
};